/*
 * Filename:    AuditDispatchController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-10
 */
package com.paic.mhis.hcpms.audit.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.util.HttpProcessUtil;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.audit.biz.service.AuditOpinionService;
import com.paic.mhis.hcpms.audit.biz.service.IAuditReviewResultSurvey;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.audit.dto.AuditDispatchUserRequestDTO;
import com.paic.mhis.hcpms.audit.dto.AuditDispatchUserResponseDTO;
import com.paic.mhis.hcpms.audit.dto.AuditOpinionDTO;
import com.paic.mhis.hcpms.common.storage.biz.service.HcpmsStorageService;
import com.paic.mhis.hcpms.common.storage.dto.HcpmsStorageDTO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

/**
 * 该控制器主要是对审核岗位 中对调查结果进行审核
 * 
 * @author EX-WANGKUI001
 * 
 */
@Controller
@RequestMapping("/auditReviewResultSurvey/")
public class AuditReviewResultSurveyController {

	@BizServicesProxy(beanName = "auditOpinionServiceImpl")
	private AuditOpinionService auditOpinionService;

	@BizServicesProxy(beanName = "auditReviewResultSurveyServiceImpl")
	private IAuditReviewResultSurvey reviewResultSurveyService;

	@BizServicesProxy(beanName = "hcpmsStorageService")
	private HcpmsStorageService hcpmsStorageService; // 暂存处理

	/**
	 * 初始化审核意见
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "processTempSave.do", method = RequestMethod.POST)
	@ResponseBody
	public AuditDispatchUserResponseDTO processTempSave(
			HttpServletRequest request) throws Exception {
		AuditDispatchUserResponseDTO responseBean = new AuditDispatchUserResponseDTO();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			AuditDispatchUserRequestDTO vo = JSONObject.toJavaObject(
					transferJson, AuditDispatchUserRequestDTO.class);
			String type = vo.getType();
			vo.setOpertionUsername(user.getUsername());
			responseBean = this.reviewResultSurveyService.processTempSave(vo,
					type, transferJson, user.getUsername());
			responseBean.setFlag(true);
		} catch (Exception e) {
			responseBean.setFlag(false);
		}
		return responseBean;
	}

	/**
	 * 加载事件处理信息和调查结果信息
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadProblemOpinionInfo.do")
	@ResponseBody
	public AuditDispatchUserResponseDTO loadProblemDealOpinion(@ModelAttribute
	AuditDispatchUserRequestDTO vo, HttpServletRequest request) {

		AuditOpinionDTO parameter = new AuditOpinionDTO();
		parameter.setProblemNo(vo.getProblemNo());
		parameter.setTaskType(AuditConstants.taskType_01);
		parameter.setIsDeleted(AuditConstants.isDeleted);

		parameter.setOpinionType(AuditConstants.opinionType_1);

		AuditOpinionDTO treatmentOpinion = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		parameter.setOpinionType(AuditConstants.opinionType_2);

		AuditOpinionDTO auditOpinion = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		parameter.setOpinionType(AuditConstants.opinionType_3);

		AuditOpinionDTO surveyOpinion = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		parameter.setOpinionType(AuditConstants.opinionType_4);

		AuditOpinionDTO surveyAuditOpinion = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");

		HcpmsStorageDTO tempObject = new HcpmsStorageDTO();
		tempObject.setBusinessId(vo.getProblemNo());
		tempObject.setModuleType(AuditConstants.sjModule);
		tempObject.setChildModuleType(AuditConstants.examine);
		tempObject.setBusinessType(AuditConstants.opinionType_5);
		tempObject.setUserId(user.getUsername());

		Object obj = this.hcpmsStorageService.getStorageDataMap(tempObject);
		if (obj != null) {
			AuditDispatchUserRequestDTO currentObj = JSONObject.toJavaObject(
					((JSONObject) obj), AuditDispatchUserRequestDTO.class);
			surveyAuditOpinion.setOpinionDesc(currentObj.getReviewResult());
			surveyOpinion.setOpinionDesc(currentObj.getSurveyOpinion());

		}

		AuditDispatchUserResponseDTO bean = new AuditDispatchUserResponseDTO();
		bean.setTreatmentOpinion(treatmentOpinion);
		bean.setAuditOpinion(auditOpinion);
		bean.setSurveyOpinion(surveyOpinion);
		bean.setSurveyAuditOpinion(surveyAuditOpinion);

		return bean;
	}

	/**
	 * 审核岗位 针对审核岗位意见为：转预警，或者同意调查终止的情况，点击页面保存按钮的后台处理逻辑
	 * 0-重新调查1-同意稽核2-同意调查终止3-违规证据不足，提请预警跟踪处理
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "processConfirmOpinion.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> processConfirmOpinion(HttpServletRequest request)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			AuditDispatchUserRequestDTO vo = JSONObject.toJavaObject(
					transferJson, AuditDispatchUserRequestDTO.class);

			String username = user.getUsername();
			vo.setOpertionUsername(username);
			vo.setUser(user);
			result = this.reviewResultSurveyService.auditSurveyResultDisagree(
					vo, transferJson, user, result);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 审核岗 在分配任务的时候 选择了经办刚A 和经办刚B 后点击确定按钮后台处理逻辑 1.A岗位 需要稽核 B岗位同意 C岗位同意 分配人员做稽核
	 * 2.A岗位 需要稽核 B岗位同意 C岗位重新调查 分配人员做调查 3.A岗位 需要稽核 B岗位保留意见 C岗位重新调查 分配人员做调查 4.A岗位
	 * 不需要稽核 B岗位同意 C岗位重新调查 分配人员做调查 5.A岗位 不需要稽核 B岗位保留意见 C岗位重新调查 分配人员做调查
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "auditProblemConfirmDispatch.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> auditProblemConfirmDispatch(
			HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			AuditDispatchUserRequestDTO vo = JSONObject.toJavaObject(
					transferJson, AuditDispatchUserRequestDTO.class);

			String username = user.getUsername();
			vo.setOpertionUsername(username);
			vo.setUser(user);
			this.reviewResultSurveyService.auditSurveyResultTask(vo, user,
					transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 调查审核，批量提交
	 * 
	 */
	@RequestMapping("researchCheckBatchSubmit.do")
	@ResponseBody
	public Map<String, Object> caseBatchSubmit(HttpServletRequest request)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		String[] listCodes = request.getParameterValues("problemCodes");
		String opinion = request.getParameter("opinion");
		String opinionDesc = request.getParameter("opinionDesc");
		try {
			result = this.reviewResultSurveyService.researchCheckBatchSubmit(
					listCodes, user, opinion, opinionDesc);
		} catch (Exception e) {
			result.put("state", AuditConstants.FLAG_FALSE);
			result.put("msg", AuditConstants.OPERATE_FAILED);
		}
		return result;
	}

}
